package cn.kc.jdbc.dao;

import cn.kc.jdbc.entity.Course;
import cn.kc.jdbc.utils.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @author mosin
 * date 2022/2/14
 * @version 1.0
 */
public class CourseDaoImpl implements CourseDao {

    @Override
    public Course findCourseByCourseName(String courseName) {
        DataSource dataSource = DruidUtil.getDataSource();
        QueryRunner runner = new QueryRunner(dataSource);
        String  sql = "select course_id as courseId from course  where course_name = ?";
        try {
            Course course = runner.query(sql, new BeanHandler<>(Course.class), courseName);
            return  course;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
